package com.hutianao.gqgl.verify.service.impl;

import com.hutianao.gqgl.spring.util.DataUitl;
import com.hutianao.gqgl.verify.service.VerifyService;
import org.mybatis.logging.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.logging.Logger;

@Service
public class VerifyServiceImpl implements VerifyService {
    @Override
    public ResponseEntity<byte[]> getLoginVfcCode(HttpServletRequest request) {

        String vfcCode = DataUitl.getVfcCode(4);

        request.getSession().setAttribute("vfcCode",vfcCode);
        byte[] contents = DataUitl.getVfcImg(vfcCode);
        HttpHeaders headers =
                new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//设置MIME类型

        return new ResponseEntity<byte[]>(
                contents,
                headers,
                HttpStatus.OK);
    }

    @Override
    public boolean authVfcCode(String vfcCode, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String isAuthCode = (String) session.getAttribute("isAuthCode");
        if("false".equals(isAuthCode)){
            return true;
        }
        String vfcCodeSession = (String) session.getAttribute("vfcCode");
        if(vfcCode.equals(vfcCodeSession)){
            return true;
        }
        return false;
    }
}

